#include "EntityPlayer.h"
#include "Visualisation.h"
#include "WorldModel.h"
#include "Sound.h"
#include "tools.h"
CEntityPlayer::CEntityPlayer(int gfxIndex, D3DXVECTOR3 initialPosition, float initialPitch, float initialYaw, float initialRoll, float speed, 
							 float health, bool collision): CWorldEntity(), m_totalHealth(health), m_currentHealth(health), m_offsetYaw((float)3.14), 
							 m_interpolationStartTime(0), m_interpolationDuration(500),m_sensitivityMouse((float)0.005)
{
	m_gfxIndex=gfxIndex;
	m_position=initialPosition;
	m_position.y=(float)(WORLDMODEL->terrainFollowing(&m_position)+1.2);
	m_interpolationFinalPosition=m_position;
	m_interpolationPreviousPosition=m_position;
	m_pitch=initialPitch;
	m_yaw=initialYaw;
	m_roll=initialRoll;
	m_previousPitch=m_pitch;
	m_previousYaw=m_yaw;
	m_previousRoll=m_roll;
	m_speed=speed;
	m_detectCollision=collision;
	calculBoundingBox();
	calculOBBBoundingBox();
	calculBoundingBoxIntoWorldSpace();

	m_lastTimeSoundPlayed=0; 
	m_indexSound=4; 
	m_durationSound=1000;
}

CEntityPlayer::~CEntityPlayer(void)
{
}

void CEntityPlayer::doUpdate(void)
{
	m_position.y=(float)(WORLDMODEL->terrainFollowing(&m_position)+1.1);

	//calcul the collision rectangle and collision
	calculBoundingBoxIntoWorldSpace();

	CWorldEntity* collisionEntity=WORLDMODEL->checkCollision(this);
	if(collisionEntity!=0)//if collision
	{
		m_position=m_previousPosition;
		m_pitch=m_previousPitch;
		m_yaw=m_previousYaw;
		m_roll=m_previousRoll;
		m_interpolationFinalPosition=m_position;

		//decrease the health
		float damage=collisionEntity->getDamage();
		if(damage>0)
		{
			m_currentHealth-=damage;
		
			//play sound
			if(m_lastTimeSoundPlayed==0)
			{
				SOUND->playSound(m_indexSound);
				m_lastTimeSoundPlayed=timeGetTime();
			}

			if(timeGetTime()-m_lastTimeSoundPlayed>=m_durationSound)
			{
				SOUND->playSound(m_indexSound);
				m_lastTimeSoundPlayed=timeGetTime();
			}
		}
	}
	else
	{
		m_previousPosition=m_position;
		m_previousPitch=m_pitch;
		m_previousYaw=m_yaw;
		m_previousRoll=m_roll;
	}
}
void CEntityPlayer::doRender(void)
{
	if(WORLDMODEL->getCameraMode()==CWorldModel::eModeTPS)
	{
		VIS->displayModel(m_gfxIndex, m_position, m_pitch, m_yaw+m_offsetYaw, m_roll);
	}	
}

D3DXVECTOR3 CEntityPlayer::getPosition()const
{
	return m_position;
}

float CEntityPlayer::getPitch()const
{
	return m_pitch;
}

float CEntityPlayer::getYaw()const
{
	return m_yaw;
}

float CEntityPlayer::getRoll()const
{
	return m_roll;
}

float CEntityPlayer::getOffsetYaw()const
{
	return m_offsetYaw;
}

float CEntityPlayer::getHealth()const
{
	return m_currentHealth;
}

void CEntityPlayer::moveForward(float amount)
{
	//direction vector
	D3DXVECTOR3 look(0, 0, 1);
	D3DXVECTOR3 up(0, 1, 0);
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis(&yawMatrix, &up, m_yaw);
	D3DXVec3TransformCoord(&look, &look, &yawMatrix);
	D3DXVec3Normalize(&look, &look);

	m_position+=m_speed*amount*look;
}
void CEntityPlayer::moveBackward(float amount)
{
	//direction vector
	D3DXVECTOR3 look(0, 0, 1);
	D3DXVECTOR3 up(0, 1, 0);
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis(&yawMatrix, &up, m_yaw);
	D3DXVec3TransformCoord(&look, &look, &yawMatrix);
	D3DXVec3Normalize(&look, &look);

	m_position-=m_speed*amount*look;
}
void CEntityPlayer::strafeLeft(float amount)
{
	//right vector
	D3DXVECTOR3 right(1, 0, 0);
	D3DXVECTOR3 up(0, 1, 0);
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis(&yawMatrix, &up, m_yaw);
	D3DXVec3TransformCoord(&right, &right, &yawMatrix);
	D3DXVec3Normalize(&right, &right);

	m_position-=m_speed*amount*right;
}
void CEntityPlayer::strafeRight(float amount)
{
	//right vector
	D3DXVECTOR3 right(1, 0, 0);
	D3DXVECTOR3 up(0, 1, 0);
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis(&yawMatrix, &up, m_yaw);
	D3DXVec3TransformCoord(&right, &right, &yawMatrix);
	D3DXVec3Normalize(&right, &right);

	m_position+=m_speed*amount*right;
}

void CEntityPlayer::turnLeftRight(float amount)
{
	m_yaw+=m_sensitivityMouse*amount;
}